package com.example.websocket.config;

import java.io.Serializable;

/**
 * 响应结果集
 *
 * @author raos
 * @date 2024-06-10
 */
@SuppressWarnings("all")
public class ResultVo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /*** 成功码 */
    public static final int SUCCESS = 0;
    public static final String SUCCESS_MSG = "操作成功！";

    /*** 失败码 */
    public static final int FAIL = 500;
    public static final String FAIL_MSG = "操作失败！";

    /*** 响应码 */
    private int code;
    /*** 响应业务类型 */
    private int type;
    /*** 响应消息 */
    private String msg;
    /*** 响应数据体 */
    private T data;

    public static <T> ResultVo<T> ok() {
        return restResult(SUCCESS, SocketConstant.GENERIC_MSG_TYPE, SUCCESS_MSG, null);
    }

    public static <T> ResultVo<T> ok(T data) {
        return restResult(SUCCESS, SocketConstant.GENERIC_MSG_TYPE, SUCCESS_MSG, data);
    }

    public static <T> ResultVo<T> ok(int type, T data) {
        return restResult(SUCCESS, type, SUCCESS_MSG, data);
    }

    public static <T> ResultVo<T> ok(int type, String msg, T data) {
        return restResult(SUCCESS, type, msg, data);
    }

    public static <T> ResultVo<T> error() {
        return restResult(FAIL, SocketConstant.GENERIC_MSG_TYPE, FAIL_MSG, null);
    }

    public static <T> ResultVo<T> error(int type) {
        return restResult(FAIL, type, FAIL_MSG, null);
    }

    public static <T> ResultVo<T> error(String msg) {
        return restResult(FAIL, SocketConstant.GENERIC_MSG_TYPE, msg, null);
    }

    public static <T> ResultVo<T> error(int type, String msg) {
        return restResult(FAIL, type, msg, null);
    }

    private static <T> ResultVo<T> restResult(int code, int type, String msg, T data) {
        ResultVo<T> resultVo = new ResultVo<>();
        resultVo.setCode(code);
        resultVo.setType(type);
        resultVo.setMsg(msg);
        resultVo.setData(data);
        return resultVo;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
